﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;

namespace Chat_WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SignalRTestController : ControllerBase
    {
        private readonly IHubContext<SignalRHub> _hubContext;
        public SignalRTestController(IHubContext<SignalRHub> hubClients)
        {
            _hubContext = hubClients;
        }
        [HttpGet("index")]
        public string index()
        {
            return "HELLO World";
        }
        /// <summary>
        /// 广播
        /// </summary>
        [HttpGet("sendall")]
        public void SendAll()
        {
            //给所有人推送消息
            _hubContext.Clients.All.SendAsync("toall", "后端", "你好", "给所有人发");
        }
        /// <summary>
        /// 单播
        /// </summary>
        /// <param name="user"></param>
        [HttpGet("sendToUser")]
        public void SendToUser(string user)
        {
            //给指定人推送消息
            _hubContext.Clients.Client(user).SendAsync("toall", "后端", $"你好{user}", "只给你发");
        }
    }
}
